home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Brailler 0.5ß / Brailler 0.5ß.source / brlr ƒ / brlr main window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  10.1 KB  |  421 lines  |  [TEXT/MMCC]

  1. #include "brlr main window.h"
  2. #include "brlr load-save.h"
  3. #include "brlr conversion.h"
  4. #include "brlr grade 2 meat.h"
  5. #include "environment.h"
  6. #include "menus.h"
  7. #include "main.h"
  8. #include "util.h"
  9. #include "text twiddling.h"
  10. #include "dialogs.h"
  11. #include "graphics.h"
  12. #include "window layer.h"
  13. #include "program globals.h"
  14.  
  15. static    void DealWithKeyPressed(WindowPtr theWindow, unsigned char theChar);
  16. static    void DealWithKeys(void);
  17.  
  18. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  19. enum { key_PageUp=0x0b, key_PageDown };
  20. enum { key_Home=0x01 };
  21. enum { key_End=0x04 };
  22.  
  23. #define kGrowBoxSize        15
  24. #define kBrailleFont        10158    /* resource # of braille font */
  25.  
  26. static    short            gOldForegroundTime;        /* stored foreground wait time */
  27. static    CursHandle        gMyIBeamHandle;
  28. static    Boolean            gIsActive;
  29. static    Boolean            gCharInProgress;
  30. static    unsigned char    gTheChar;
  31.  
  32. static    Boolean            gSetupDone=FALSE;
  33.  
  34. void SetupTheMainWindow(WindowPtr theWindow)
  35. {
  36.     unsigned char    *titleStr="\puntitled";
  37.     Point            topLeft;
  38.     FSSpec            fs;
  39.     
  40.     SetWindowHeight(theWindow, qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28);
  41.     SetWindowWidth(theWindow, qd.screenBits.bounds.right-qd.screenBits.bounds.left-70);
  42.     SetWindowType(theWindow, zoomDocProc);
  43.     topLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  44.     topLeft.h=qd.screenBits.bounds.left+10;
  45.     SetWindowTopLeft(theWindow, topLeft);
  46.     SetWindowHasCloseBox(theWindow, TRUE);
  47.     SetWindowMaxDepth(theWindow, 1);
  48.     SetWindowDepth(theWindow, 1);
  49.     SetWindowIsFloat(theWindow, FALSE);
  50.     SetWindowTitle(theWindow, titleStr);
  51.     SetWindowAutoCenter(theWindow, FALSE);
  52.     fs.name[0]=0x00;
  53.     fs.vRefNum=0;
  54.     fs.parID=0;
  55.     SetWindowFS(theWindow, fs);
  56.     SetWindowIsModified(theWindow, FALSE);
  57.     
  58.     gTheChar=0x00;
  59.     gCharInProgress=FALSE;
  60.     
  61.     if (gGrade==2)
  62.         OpenTheIndWindow(kFloatingWindow);
  63.     
  64.     if (gSetupDone)
  65.         return;
  66.     
  67.     gMyIBeamHandle=GetCursor(iBeamCursor);
  68.     gSetupDone=TRUE;
  69. }
  70.  
  71. void ShutDownTheMainWindow(void)
  72. {
  73.     ReleaseResource((Handle)gMyIBeamHandle);
  74. }
  75.  
  76. void OpenTheMainWindow(WindowPtr theWindow)
  77. {
  78.     TEHandle        hTE;
  79.     FontInfo        theFontInfo;
  80.     Rect            vScrollBarRect, hScrollBarRect;
  81.     Rect            destRect, viewRect;
  82.     
  83.     hTE=GetWindowTE(theWindow);
  84.     if (hTE==0L)
  85.     {
  86.         SetRect(&vScrollBarRect, GetWindowWidth(theWindow)-kGrowBoxSize, -1,
  87.             GetWindowWidth(theWindow)+1, GetWindowHeight(theWindow)+1-kGrowBoxSize);
  88.         SetRect(&hScrollBarRect, -1, GetWindowHeight(theWindow)-kGrowBoxSize,
  89.             GetWindowWidth(theWindow)-kGrowBoxSize+1, GetWindowHeight(theWindow)+1);
  90.         SetWindowVScrollBar(theWindow,
  91.             NewControl(theWindow, &vScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  92.         SetWindowHScrollBar(theWindow,
  93.             NewControl(theWindow, &hScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  94.         
  95.         GetTERect(theWindow, &destRect, TRUE);
  96.         viewRect=destRect;
  97.         hTE=TENew(&destRect, &viewRect);
  98.         SetWindowTE(theWindow, hTE);
  99.         TextFont((**hTE).txFont=kBrailleFont);
  100.         TextSize((**hTE).txSize=24);
  101.         TextFace((**hTE).txFace=0);
  102.         GetFontInfo(&theFontInfo);
  103.         (**hTE).fontAscent=theFontInfo.ascent+5;
  104.         (**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading+5;
  105.         AdjustViewRect(hTE);
  106.         TEAutoView(TRUE, hTE);
  107.         TESetClickLoop((ProcPtr)MyClikLoop, hTE);
  108.     }
  109.     
  110.     gIsActive=TRUE;
  111.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  112.     AdjustMenus();
  113. }
  114.  
  115. void IdleInMainWindow(WindowPtr theWindow, Point mouseLoc)
  116. {
  117.     KeyMap            rawKeys;
  118.     unsigned short    theKeys[8];
  119.     TEHandle        hTE;
  120.     unsigned char    theChar;
  121.     
  122.     if (gInProgress)
  123.         return;
  124.     
  125.     GetKeys(rawKeys);
  126.     Mymemcpy((Ptr)theKeys, (Ptr)rawKeys, sizeof(rawKeys));
  127.     hTE=GetWindowTE(theWindow);
  128.     TEIdle(hTE);
  129.     if ((theKeys[0]) || (theKeys[1]) || (theKeys[2]) || (theKeys[3]&0xfffd) ||
  130.         (theKeys[4]) || (theKeys[5]) || (theKeys[6]) || (theKeys[7]))
  131.     {}
  132.     else if (gCharInProgress)
  133.     {
  134.         theChar=gTheChar+' ';
  135.         gTheChar=0x00;
  136.         gCharInProgress=FALSE;
  137.         DealWithKeyPressed(theWindow, theChar);
  138.     }
  139.     
  140.     if (PtInRect(mouseLoc, &((**hTE).viewRect)))
  141.     {
  142.         if (!gCustomCursor)
  143.         {
  144.             SetCursor(*gMyIBeamHandle);
  145.             gCustomCursor=TRUE;
  146.         }
  147.     }
  148.     else
  149.     {
  150.         gCustomCursor=FALSE;
  151.     }
  152. }
  153.  
  154. void KeyPressedInMainWindow(WindowPtr theWindow, unsigned char theChar)
  155. {
  156.     TEHandle        hTE;
  157.     ControlHandle    vScrollBar;
  158.     
  159.     if (gInProgress)
  160.         return;
  161.     
  162.     hTE=GetWindowTE(theWindow);
  163.     vScrollBar=GetWindowVScrollBar(theWindow);
  164.     
  165.     if (gPostingEvents)
  166.     {
  167.         DealWithKeyPressed(theWindow, theChar);
  168.         gCharsToPost--;
  169.         if (gCharsToPost==0)
  170.             gPostingEvents=FALSE;
  171.     }
  172.     else
  173.     {
  174.         switch (theChar)
  175.         {
  176.             case key_PageUp:
  177.                 ScrollActionProc(vScrollBar, inPageUp);
  178.                 break;
  179.             case key_PageDown:
  180.                 ScrollActionProc(vScrollBar, inPageDown);
  181.                 break;
  182.             case key_Home:
  183.                 TEPinScroll(0, TEGetHeight((**hTE).nLines, 1, hTE), hTE);
  184.                 break;
  185.             case key_End:
  186.                 TEPinScroll(0, -TEGetHeight((**hTE).nLines, 1, hTE), hTE);
  187.                 break;
  188.             case ' ':
  189.             case '\r':
  190.             case 0x08:
  191.             case 0x03:
  192.             case key_LeftArrow:
  193.             case key_RightArrow:
  194.             case key_UpArrow:
  195.             case key_DownArrow:
  196.                 DealWithKeyPressed(theWindow, theChar);
  197.                 break;
  198.             default:
  199.                 if (gGrade==1)
  200.                 {
  201.                     short            time, oldTime;
  202.                     unsigned char    brailleChar;
  203.                     
  204.                     time=1;
  205.                     do
  206.                     {
  207.                         oldTime=time;
  208.                         brailleChar=DealWithLetter(theChar, &time);
  209.                         if (brailleChar!=0x00)
  210.                             DealWithKeyPressed(theWindow, brailleChar);
  211.                     }
  212.                     while (oldTime!=time);
  213.                 }
  214.                 else
  215.                     DealWithKeys();
  216.                 break;
  217.         }
  218.     }
  219.     
  220.     AdjustVScrollBar(vScrollBar, hTE);
  221. }
  222.  
  223. void MouseClickedInMainWindow(WindowPtr theWindow, Point thePoint)
  224. {
  225.     short            partCode;
  226.     ControlHandle    theControl;
  227.     short            scrollDistance;
  228.     short            oldSetting;
  229.     ControlActionUPP    scrollActionUPP=NewControlActionProc(ScrollActionProc);
  230.     TEHandle        hTE;
  231.     
  232.     if (gInProgress)
  233.         return;
  234.     
  235.     hTE=GetWindowTE(theWindow);
  236.     
  237.     if (PtInRect(thePoint, &((**hTE).viewRect)))
  238.     {
  239.         TEClick(thePoint, (GetTheModifiers()&512) ? TRUE : FALSE, hTE);
  240.     }
  241.     else
  242.     {
  243.         partCode=FindControl(thePoint, theWindow, &theControl);
  244.         if (theControl==GetWindowVScrollBar(theWindow))
  245.         {
  246.             switch (partCode)
  247.             {
  248.                 case inThumb:
  249.                     oldSetting=GetControlValue(theControl);
  250.                     partCode=TrackControl(theControl, thePoint, 0L);
  251.                     if (partCode==inThumb)
  252.                     {
  253.                         scrollDistance=oldSetting-GetControlValue(theControl);
  254.                         if (scrollDistance!=0)
  255.                             TEPinScroll(0, scrollDistance*(**hTE).lineHeight, hTE);
  256.                     }
  257.                     break;
  258.                 case inUpButton:
  259.                 case inDownButton:
  260.                 case inPageUp:
  261.                 case inPageDown:
  262.                     partCode=TrackControl(theControl, thePoint, scrollActionUPP);
  263.                     break;
  264.             }
  265.         }
  266.     }
  267. }
  268.  
  269. Boolean CloseTheMainWindow(WindowPtr theWindow)
  270. {
  271.     WindowPtr        floatingWindow;
  272.     ModalFilterUPP    procFilter = NewModalFilterProc(ThreeButtonFilter);
  273.     short            result;
  274.     
  275.     if (WindowIsModifiedQQ(theWindow))
  276.     {
  277.         SetCursor(&qd.arrow);
  278.         PositionDialog('ALRT', saveAlert);
  279.         ParamText(GetWindowTitle(theWindow), "\p", "\p", "\p");
  280.         result=Alert(saveAlert, procFilter);
  281.         DisposeRoutineDescriptor(procFilter);
  282.         switch (result)
  283.         {
  284.             case 1:    /* save */
  285.                 LoadSaveDispatch(FALSE, TRUE);
  286.                 if (WindowIsModifiedQQ(theWindow))    /* save didn't work for some reason */
  287.                     return FALSE;
  288.                 break;
  289.             case 2:     /* cancel */
  290.                 return FALSE;
  291.                 break;
  292.             case 3:    /* don't save */
  293.                 break;
  294.         }
  295.     }
  296.     
  297.     floatingWindow=GetIndWindowPtr(kFloatingWindow);
  298.     if (floatingWindow!=0L)
  299.         CloseTheWindow(floatingWindow);
  300.     
  301.     gCustomCursor=FALSE;
  302.     
  303.     return TRUE;
  304. }
  305.  
  306. void DisposeTheMainWindow(WindowPtr theWindow)
  307. {
  308.     TEHandle        hTE;
  309.     
  310.     hTE=GetWindowTE(theWindow);
  311.     if (hTE!=0L)
  312.     {
  313.         TEDispose(hTE);
  314.         SetWindowTE(theWindow, 0L);
  315.     }
  316. }
  317.  
  318. void ActivateTheMainWindow(WindowPtr theWindow)
  319. {
  320.     gOldForegroundTime=gForegroundWaitTime;
  321.     gForegroundWaitTime=0;
  322.     TEActivate(GetWindowTE(theWindow));
  323.     gIsActive=TRUE;
  324.     HiliteControl(GetWindowVScrollBar(theWindow), 0);
  325.     HiliteControl(GetWindowHScrollBar(theWindow), 0);
  326.     DrawGrowIcon(theWindow);
  327. }
  328.  
  329. void DeactivateTheMainWindow(WindowPtr theWindow)
  330. {
  331.     Rect            tempRect;
  332.     
  333.     gForegroundWaitTime=gOldForegroundTime;
  334.     TEDeactivate(GetWindowTE(theWindow));
  335.     gIsActive=FALSE;
  336.     gCustomCursor=FALSE;
  337.     HiliteControl(GetWindowVScrollBar(theWindow), 255);
  338.     HiliteControl(GetWindowHScrollBar(theWindow), 255);
  339.     tempRect.bottom=theWindow->portRect.bottom;
  340.     tempRect.right=theWindow->portRect.right;
  341.     tempRect.left=tempRect.right-kGrowBoxSize+1;
  342.     tempRect.top=tempRect.bottom-kGrowBoxSize+1;
  343.     EraseRect(&tempRect);
  344. }
  345.  
  346. void CopybitsTheMainWindow(WindowPtr theWindow, WindowPtr offscreenWindowPtr)
  347. {
  348.     Rect            tempRect;
  349.     
  350.     if (gIsActive)
  351.     {
  352.         DrawGrowIcon(theWindow);
  353.     }
  354.     else
  355.     {
  356.         tempRect.bottom=theWindow->portRect.bottom;
  357.         tempRect.right=theWindow->portRect.right;
  358.         tempRect.left=tempRect.right-kGrowBoxSize+1;
  359.         tempRect.top=tempRect.bottom-kGrowBoxSize+1;
  360.         EraseRect(&tempRect);
  361.     }
  362.     
  363.     UpdateControls(theWindow, theWindow->visRgn);
  364.     
  365.     tempRect=theWindow->portRect;
  366.     tempRect.bottom-=kGrowBoxSize;
  367.     tempRect.right-=kGrowBoxSize;
  368.     CopyBits(    &(offscreenWindowPtr->portBits),
  369.                 &(theWindow->portBits),
  370.                 &tempRect, &tempRect, 0, 0L);
  371. }
  372.  
  373. void ResizeControlsInMainWindow(WindowPtr theWindow)
  374. {
  375.     TEHandle        hTE;
  376.     ControlHandle    vScrollBar, hScrollBar;
  377.     
  378.     hTE=GetWindowTE(theWindow);
  379.     vScrollBar=GetWindowVScrollBar(theWindow);
  380.     hScrollBar=GetWindowHScrollBar(theWindow);
  381.     AdjustScrollSizes(theWindow, hTE, vScrollBar, hScrollBar);
  382.     AdjustViewRect(hTE);
  383.     TECalText(hTE);
  384.     AdjustForEndScroll(vScrollBar, hTE);
  385.     AdjustVScrollBar(vScrollBar, hTE);
  386. }
  387.  
  388. void GetGrowSizeMainWindow(WindowPtr theWindow, Rect *sizeRect)
  389. {
  390.     SetRect(sizeRect, 200, 48+kGrowBoxSize+1, 32766, 32767);
  391. }
  392.  
  393. /* ---------------------------------------------------- */
  394. /* the rest of these are internal to brlr main window.c */
  395.  
  396. static    void DealWithKeyPressed(WindowPtr theWindow, unsigned char theChar)
  397. {
  398.     TEHandle        hTE;
  399.     
  400.     hTE=GetWindowTE(theWindow);
  401.     TEKey(theChar, hTE);
  402.     TESelView(hTE);
  403.     AdjustForEndScroll(GetWindowVScrollBar(theWindow), hTE);
  404.     SetWindowIsModified(theWindow, TRUE);
  405. }
  406.  
  407. static    void DealWithKeys(void)
  408. {
  409.     KeyMap            rawKeys;
  410.     unsigned short    theKeys[8];
  411.     
  412.     GetKeys(rawKeys);
  413.     Mymemcpy((Ptr)theKeys, (Ptr)rawKeys, sizeof(rawKeys));
  414.     if (theKeys[0]&16384)    { gTheChar|=0x04; gCharInProgress=TRUE; }
  415.     if (theKeys[0]&32768)    { gTheChar|=0x02; gCharInProgress=TRUE; }
  416.     if (theKeys[0]&1)        { gTheChar|=0x01; gCharInProgress=TRUE; }
  417.     if (theKeys[2]&8)        { gTheChar|=0x08; gCharInProgress=TRUE; }
  418.     if (theKeys[2]&128)        { gTheChar|=0x10; gCharInProgress=TRUE; }
  419.     if (theKeys[2]&16)        { gTheChar|=0x20; gCharInProgress=TRUE; }
  420. }
  421.